package cn.iocoder.yudao.module.pickbook.service.user;

import cn.iocoder.yudao.module.pickbook.dal.dataobject.user.PickbookUserDO;
import cn.iocoder.yudao.module.pickbook.dal.mysql.user.PickbookUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class PickbookUserServiceImpl implements PickbookUserService {

    @Resource
    private PickbookUserMapper userMapper;

    @Override
    public PickbookUserDO getUserByOpenid(String openid) {
        return userMapper.selectByOpenid(openid);
    }

    @Override
    public PickbookUserDO getUserByUnionid(String unionid) {
        return userMapper.selectByUnionid(unionid);
    }

    @Override
    public PickbookUserDO createUser(String openid, String unionid, String nickname, String avatar) {
        // 创建新用户
        PickbookUserDO user = PickbookUserDO.builder()
                .openid(openid)
                .unionid(unionid)
                .nickname(nickname)
                .avatar(avatar)
                .role("USER") // 默认角色
                .status(1) // 正常状态
                .isMember(false) // 默认非会员
                .notification(1) // 默认开启通知
                .autoPlay(1) // 默认开启自动播放
                .build();
        
        // 保存到数据库
        userMapper.insert(user);
        
        log.info("新用户创建成功，OpenID: {}, 昵称: {}", openid, nickname);
        return user;
    }

    @Override
    public void updateUser(PickbookUserDO user) {
        userMapper.updateById(user);
        log.info("用户信息更新成功，用户ID: {}", user.getId());
    }

    @Override
    public PickbookUserDO getUser(Long id) {
        return userMapper.selectById(id);
    }
} 